package com.ctp.thread.active;

import javax.swing.table.DefaultTableModel;

import com.ctp.bean.AccountBean;
import com.ctp.controller.manager.TraderManager;
import com.ctp.socket.util.AccountUtil;

public class AccountUpdateThread extends Thread implements Runnable {

	private DefaultTableModel tModel = null;

	private TraderManager traderManager = TraderManager.instance();

	public void setTableModel(DefaultTableModel tModel) {
		this.tModel = tModel;

		AccountBean[] accts = new AccountBean[tModel.getRowCount()];
		for (int i = 0; i < accts.length; i++) {
			accts[i] = new AccountBean();
		}

		traderManager.setAccts(accts);
	}

	public void run() {
		AccountBean[] accts = traderManager.getAccts();

		while (true) {
			for (int i = 0; i < tModel.getRowCount(); i++) {
				String acct = (String) tModel.getValueAt(i, 0);
				String[] params = AccountUtil.getAccountParams(acct);

				if (params != null) {
					accts[i].setAccountBean(params);
					for (int j = 1; j < tModel.getColumnCount(); j++) {
						tModel.setValueAt(params[j], i, j);
					}
				}
			}

			try {
				Thread.sleep(1000);
			} catch (Exception e) {
			}
		}
	}

}
